package spire;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SubSuperScript;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.TextRange;
import com.spire.doc.fields.shape.Emphasis;

import java.awt.*;

public class wordLearn {

    public static void main(String[] args) {

        //创建一个Document类的对象
        Document document = new Document();

        //添加一个节
        Section sec = document.addSection();

        //添加一个段落
        Paragraph paragraph = sec.addParagraph();
        paragraph.appendText("\r\n以下是一些文字格式设置：\r\n");
        paragraph.getStyle().getCharacterFormat().setFontName("HarmonyOS Sans SC");

        //在段落中添加文本并返回TextRange对象
        TextRange tr = paragraph.appendText("带删除线的文字");

        //通过TextRange对象将字符格式设置为带删除线
        tr.getCharacterFormat().isStrikeout(true);

        //应用阴影效果
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("带阴影的文字");
        tr.getCharacterFormat().isShadow (true);

        //设置字号
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("字号更大的文字");
        tr.getCharacterFormat().setFontSize(20);

        //设置字体名称
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("楷体文字");
        tr.getCharacterFormat().setFontName("楷体");

        //设置字体颜色
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("红色文字");
        tr.getCharacterFormat().setTextColor(Color.red);

        //设置加粗、斜体
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("加粗、斜体文字");
        tr.getCharacterFormat().setBold(true);
        tr.getCharacterFormat().setItalic(true);

        //添加下划线
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("带下划线的文字");
        tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single);

        //设置突出显示颜色
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("带突出显示颜色的文字");
        tr.getCharacterFormat().setHighlightColor(Color.green);

        //添加边框
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("带边框的文字");
        tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
        tr.getCharacterFormat().getBorder().setColor(Color.black);

        //添加着重号
        paragraph.appendText("\r\n");
        tr = paragraph.appendText("带着重号的文字");
        tr.getCharacterFormat().setEmphasisMark(Emphasis.Dot_Below);

        //设置上标
        paragraph.appendText("\r\n上标： a");
        tr = paragraph.appendText("2");
        tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        paragraph.appendText(" + b");
        tr = paragraph.appendText("2");
        tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        paragraph.appendText(" = c");
        tr = paragraph.appendText("2");
        tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
        paragraph.appendText(".");

        //保存文档
        document.saveToFile("设置字体格式.docx", FileFormat.Docx);
    }
}
